package com.le.tester.book.concurrentjavafight.l7_1_1;

import java.math.BigInteger;
import java.util.concurrent.BlockingQueue;

/**
 * createTime：2022/11/3 13:46
 * description：不可靠的取消操作，将生产者
 */
public class BrokenPrimeProducer extends Thread {

    private final BlockingQueue<BigInteger> queue;

    private volatile boolean cancelled = Boolean.FALSE;

    BrokenPrimeProducer(BlockingQueue<BigInteger> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            BigInteger one = BigInteger.ONE;
            while (!cancelled) {
                queue.put(one = one.nextProbablePrime());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void cancel() {
        cancelled = true;
    }

    void consumerPrimes() {
        BlockingQueue<BigInteger> primes = null;
        BrokenPrimeProducer producer = new BrokenPrimeProducer(primes);
        producer.start();
//        try {
//            while (needMorePrime()) {
//                primes.take();
//            }
//        } finally {
//            producer.cancel();
//        }

    }
}
